package com.tr.ssocms.shiro;


import com.tr.common.bo.User;
import com.tr.ssocms.service.RemoteUserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;


public class UserRealm extends AuthorizingRealm {

    @Autowired
    private RemoteUserService remoteUserService;
    /**
     * 授权
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        User user = (User) principalCollection.getPrimaryPrincipal();
        System.out.println("登录用户"+user);
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo();

        return null;
    }

    /**
     * 认证
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String userName = authenticationToken.getPrincipal().toString();
        System.out.println("用户名"+userName);
        User realUser = remoteUserService.queryByUserName(userName);
        if(realUser == null){
            throw new AccountException();
        }
        return new SimpleAuthenticationInfo(realUser,realUser.getPassword(),getName());
    }
}
